import { Component, OnInit } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import * as moment from 'moment';
import { DATE_TIME_FORMAT } from 'app/shared/constants/input.constants';

import { IOrder, Order } from 'app/shared/model/order.model';
import { OrderService } from './order.service';

@Component({
  selector: 'jhi-order-update',
  templateUrl: './order-update.component.html',
})
export class OrderUpdateComponent implements OnInit {
  isSaving = false;

  editForm = this.fb.group({
    id: [],
    orderCode: [],
    orderTime: [],
    cancelTime: [],
    dealTime: [],
    sendTime: [],
    marginTime: [],
    payTime: [],
    checkedTime: [],
    userId: [],
    itemTypeId: [],
    itemId: [],
    orderPrice: [],
    dealPrice: [],
    isLockPrice: [],
    states: [],
    estimatesQty: [],
    dealQty: [],
  });

  constructor(protected orderService: OrderService, protected activatedRoute: ActivatedRoute, private fb: FormBuilder) {}

  ngOnInit(): void {
    this.activatedRoute.data.subscribe(({ order }) => {
      if (!order.id) {
        const today = moment().startOf('day');
        order.orderTime = today;
        order.cancelTime = today;
        order.dealTime = today;
        order.sendTime = today;
        order.marginTime = today;
        order.payTime = today;
        order.checkedTime = today;
      }

      this.updateForm(order);
    });
  }

  updateForm(order: IOrder): void {
    this.editForm.patchValue({
      id: order.id,
      orderCode: order.orderCode,
      orderTime: order.orderTime ? order.orderTime.format(DATE_TIME_FORMAT) : null,
      cancelTime: order.cancelTime ? order.cancelTime.format(DATE_TIME_FORMAT) : null,
      dealTime: order.dealTime ? order.dealTime.format(DATE_TIME_FORMAT) : null,
      sendTime: order.sendTime ? order.sendTime.format(DATE_TIME_FORMAT) : null,
      marginTime: order.marginTime ? order.marginTime.format(DATE_TIME_FORMAT) : null,
      payTime: order.payTime ? order.payTime.format(DATE_TIME_FORMAT) : null,
      checkedTime: order.checkedTime ? order.checkedTime.format(DATE_TIME_FORMAT) : null,
      userId: order.userId,
      itemTypeId: order.itemTypeId,
      itemId: order.itemId,
      orderPrice: order.orderPrice,
      dealPrice: order.dealPrice,
      isLockPrice: order.isLockPrice,
      states: order.states,
      estimatesQty: order.estimatesQty,
      dealQty: order.dealQty,
    });
  }

  previousState(): void {
    window.history.back();
  }

  save(): void {
    this.isSaving = true;
    const order = this.createFromForm();
    if (order.id !== undefined) {
      this.subscribeToSaveResponse(this.orderService.update(order));
    } else {
      this.subscribeToSaveResponse(this.orderService.create(order));
    }
  }

  private createFromForm(): IOrder {
    return {
      ...new Order(),
      id: this.editForm.get(['id'])!.value,
      orderCode: this.editForm.get(['orderCode'])!.value,
      orderTime: this.editForm.get(['orderTime'])!.value ? moment(this.editForm.get(['orderTime'])!.value, DATE_TIME_FORMAT) : undefined,
      cancelTime: this.editForm.get(['cancelTime'])!.value ? moment(this.editForm.get(['cancelTime'])!.value, DATE_TIME_FORMAT) : undefined,
      dealTime: this.editForm.get(['dealTime'])!.value ? moment(this.editForm.get(['dealTime'])!.value, DATE_TIME_FORMAT) : undefined,
      sendTime: this.editForm.get(['sendTime'])!.value ? moment(this.editForm.get(['sendTime'])!.value, DATE_TIME_FORMAT) : undefined,
      marginTime: this.editForm.get(['marginTime'])!.value ? moment(this.editForm.get(['marginTime'])!.value, DATE_TIME_FORMAT) : undefined,
      payTime: this.editForm.get(['payTime'])!.value ? moment(this.editForm.get(['payTime'])!.value, DATE_TIME_FORMAT) : undefined,
      checkedTime: this.editForm.get(['checkedTime'])!.value
        ? moment(this.editForm.get(['checkedTime'])!.value, DATE_TIME_FORMAT)
        : undefined,
      userId: this.editForm.get(['userId'])!.value,
      itemTypeId: this.editForm.get(['itemTypeId'])!.value,
      itemId: this.editForm.get(['itemId'])!.value,
      orderPrice: this.editForm.get(['orderPrice'])!.value,
      dealPrice: this.editForm.get(['dealPrice'])!.value,
      isLockPrice: this.editForm.get(['isLockPrice'])!.value,
      states: this.editForm.get(['states'])!.value,
      estimatesQty: this.editForm.get(['estimatesQty'])!.value,
      dealQty: this.editForm.get(['dealQty'])!.value,
    };
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<IOrder>>): void {
    result.subscribe(
      () => this.onSaveSuccess(),
      () => this.onSaveError()
    );
  }

  protected onSaveSuccess(): void {
    this.isSaving = false;
    this.previousState();
  }

  protected onSaveError(): void {
    this.isSaving = false;
  }
}
